# -*- coding: utf-8 -*-
from django.db import models
from tinymce import models as tinymce_models
from django.utils.translation import ugettext as _
import os
from catalog.utils import thumbnail

from tinymce import models as tinymce_models

from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^tinymce\.models\.HTMLField"])

class Section(models.Model):
    name = models.CharField(_(u'Название'), max_length=100, blank=False, null=False, help_text=_(u'Название раздела'))
    description=models.TextField(_(u'Описание'), blank=True, null=False, help_text=_(u'Описание раздела'))
    
    def __unicode__(self):
        return '%s' %  (self.name)
    
    @models.permalink
    def get_absolut_url(self):
        return ('get_section', [str(self.id)])
    
    class Meta:
        verbose_name = u"Раздел"
        verbose_name_plural = u"Разделы"

class Category(models.Model):
    id_sec = models.ForeignKey(Section, to_field='id')
    name = models.CharField(_(u'Название'), max_length=100, blank=False, null=False, help_text=_(u'Название категории'))
    description = models.TextField(_(u'Описание'), blank=True, null=False, help_text=_(u'Описание категории'))
    
    def __unicode__(self):
        return '%s' %  (self.name)
    
    @models.permalink
    def get_absolut_url(self):
        return ('get_category', [str(self.id)])
    
    class Meta:
        verbose_name = u"Категорию"
        verbose_name_plural = u"Категории"
    
class Product(models.Model):
    id_cat = models.ForeignKey(Category, to_field='id')
    name = models.CharField(_(u'Название'), max_length=100, blank=False, null=False, help_text=_(u'Название товара'))
    price = models.FloatField(_(u'Цена'), blank=False, null=False, help_text=_(u'Цена товара в рублях'))
    description = tinymce_models.HTMLField(_(u'Описание'), null=True, blank=True, help_text=_(u'Описание товара'))
    img = models.ImageField(upload_to='i/catalog')
    
    def __unicode__(self):
        return self.name
    
    @models.permalink
    def get_absolut_url(self):
        return ('get_product', [str(self.id)])
    
    class Meta:
        verbose_name = u"Товар"
        verbose_name_plural = u"Товары"
        
class Images(models.Model):
    title = models.CharField(_(u'Название'), max_length=250)
    description = models.TextField(_(u'Описание'), blank=True)
    pub_date = models.DateTimeField(_(u'Дата загрузки'), auto_now_add=True)
    image = models.ImageField(_(u'Файл'), upload_to='i/')
    
    class Meta:
        verbose_name = u"Изображение"
        verbose_name_plural = u"Изображения"
    
    def preview_image_url(self):
        image_path = thumbnail(str(self.image), '150x150')
        image_path = image_path.replace('\\','/') # Windows-Fix 
        return '<a href="'+ str(self.id) +'/"><img src="'+ str(image_path) +'"/></a>'
    
    preview_image_url.short_description = u'Изображение'
    preview_image_url.allow_tags = True
    
    def __unicode__(self):
        return str(self.id)